
/* 
定义好后,记得到serviceEnter里注册一下
*/
const mongoose = require('mongoose');

const {baseService} =require('../../baseService')
const model =require('../../../dao/model/model')
const bean =require('../../../beans/beansEnter')


/* 异常类 */
class professionAndIndustryRelativeServiceError extends Error {
    constructor(error,message='行业-职业异常'){
        super()
        this.error = error
        this.message=message
    }
}



class professionAndIndustryRelativeService extends baseService{
    static ins = new professionAndIndustryRelativeService()
    constructor(){
        super(model.modelprofessionIndustryRelative,bean.professionAndIndustryRelativeBean,professionAndIndustryRelativeServiceError)
    }

    static getIns (){
        if(this.ins){
            return this.ins
        }else{
            return new professionAndIndustryRelativeService()
        }
    }

   async  findByIndustryIdAndAggregate(industryId){
    let cc
   try {
    cc=  await  model.modelprofessionIndustryRelative.aggregate(

        [   
            {$match: { 'industryID': mongoose.Types.ObjectId(industryId)}},
            // 关联行业表
            {
                $lookup:{
                          from:"industry",
                          localField:"industryID",
                          foreignField:"_id",
                          as:"industry"},
            },

            // 关联职业表
             {
                $lookup:{
                          from:"profession",
                          localField:"professionID",
                          foreignField:"_id",
                          as:"profession"},
            },
        ]
)
    } catch (error) {
        console.log(error);
        return null        
    }
    if(cc){
        return cc
    }else {
        throw new professionAndIndustryRelativeServiceError()
    }
        
    }

    
}






module.exports={professionAndIndustryRelativeService}